using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class TipPanel : MonoBehaviour
{
    public static TipPanel Instance = null;
    private Image panel;

    private Button _button1;
    private Button _button2;

    private TextMeshProUGUI titleText;
    private TextMeshProUGUI contentText;
    private TextMeshProUGUI _button1Text;
    private TextMeshProUGUI _button2Text;

    private Action _action1;
    private Action _action2;

    public void Init()
    {
        if (Instance == null) Instance = this;
        panel = transform.Find("Panel").GetComponent<Image>();
        titleText = transform.Find("Panel/TitleText").GetComponent<TextMeshProUGUI>();
        contentText = transform.Find("Panel/ContentText").GetComponent<TextMeshProUGUI>();

        _button1 = transform.Find("Panel/Buttons/Button1").GetComponent<Button>();
        _button1.onClick.AddListener(OnButton1Click);
        _button2 = transform.Find("Panel/Buttons/Button2").GetComponent<Button>();
        _button2.onClick.AddListener(OnButton2Click);

        _button1Text = transform.Find("Panel/Buttons/Button1/Button1Text").GetComponent<TextMeshProUGUI>();
        _button2Text = transform.Find("Panel/Buttons/Button2/Button2Text").GetComponent<TextMeshProUGUI>();

        transform.Find("Mask").GetComponent<Button>().onClick.AddListener(() => HidePanel());
    }

    // public static void Show(string title = "", string content = "", Action action1 = null, Action action2 = null,
    //     string button1Str = "", string button2Str = "")
    // {
    //     GameObject go = GameManager.Instance.LoadPrefab(Constants.PathTipPanel);
    //     go.transform.SetParent(GameObject.Find("Canvas").transform);
    //     go.transform.localPosition = Vector3.zero;
    //     RectTransform rectTransform = go.GetComponent<RectTransform>();
    //     rectTransform.anchorMin = Vector2.zero;
    //     rectTransform.anchorMax = Vector2.one;
    //     rectTransform.sizeDelta = Vector2.zero;
    //
    //     var panel = go.GetComponent<TipPanel>();
    //     panel.Init();
    //     panel.ShowPanel(title, content, action1, action2, button1Str, button2Str);
    // }

    public void ShowPanel(string title = "", string content = "", Action action1 = null, Action action2 = null,
        string button1Str = "", string button2Str = "")
    {
        gameObject.SetActive(true);
        panel.transform.localScale = Vector3.zero;
        panel.transform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBounce);

        _action1 = action1;
        _action2 = action2;

        titleText.text = title;
        contentText.text = content;
        _button1.gameObject.SetActive(!string.IsNullOrEmpty(button1Str));
        _button1Text.text = button1Str;
        _button1.GetComponent<RectTransform>().sizeDelta = new Vector2(_button1Text.preferredWidth + 100, 70);
        _button2.gameObject.SetActive(!string.IsNullOrEmpty(button2Str));
        _button2Text.text = button2Str;
        _button2.GetComponent<RectTransform>().sizeDelta = new Vector2(_button2Text.preferredWidth + 100, 70);

        AdManager.ShowNative();
    }

    public void HidePanel()
    {
        gameObject.SetActive(false);
    }

    private void OnButton1Click()
    {
        GameManager.Instance.PlayBtnClickAudio();
        _action1?.Invoke();
        HidePanel();
    }

    private void OnButton2Click()
    {
        GameManager.Instance.PlayBtnClickAudio();
        _action2?.Invoke();
        HidePanel();
    }
}